package com.qiyi.player.wonder.plugins.recommend.view.part
{
   import flash.display.Sprite;
   import flash.media.SoundChannel;
   import com.qiyi.player.comps.rb.ShareBtn;
   import flash.text.TextField;
   import com.qiyi.player.comps.rb.ReviewBtn;
   import com.qiyi.player.wonder.plugins.recommend.model.RecommendVO;
   import flash.net.URLRequest;
   import flash.media.Sound;
   import flash.geom.Rectangle;
   import com.qiyi.player.wonder.plugins.recommend.RecommendDef;
   import flash.events.MouseEvent;
   import com.qiyi.player.wonder.plugins.recommend.view.RecommendEvent;
   import com.qiyi.player.wonder.common.ui.FastCreator;
   import flash.geom.Point;
   
   public class PlayFinishRecommend extends Sprite
   {
      
      public function PlayFinishRecommend(param1:Vector.<RecommendVO>, param2:Boolean = false, param3:Boolean = false) {
         this._recommendVector = new Vector.<PlayFinishRecommendItem>();
         super();
         this._recommendData = param1;
         this._isCanComment = param2;
         this._isCanShare = param3;
         if((this._recommendData) && this._recommendData.length > 0)
         {
            this.initPanel();
         }
      }
      
      private static const BTN_HEIGHT:uint = 40;
      
      private var _song:SoundChannel;
      
      private var _shareBtn:ShareBtn;
      
      private var _shareTF:TextField;
      
      private var _commonBtn:ReviewBtn;
      
      private var _commonTF:TextField;
      
      private var _recommendData:Vector.<RecommendVO>;
      
      private var _isCanComment:Boolean = false;
      
      private var _isCanShare:Boolean = false;
      
      private var _showWidth:Number = 0;
      
      private var _showHeight:Number = 0;
      
      private var _recommendVector:Vector.<PlayFinishRecommendItem>;
      
      public function get showHeight() : Number {
         return this._showHeight;
      }
      
      public function get showWidth() : Number {
         return this._showWidth;
      }
      
      private function initPanel() : void {
         var item:PlayFinishRecommendItem = null;
         var request:URLRequest = null;
         var soundFactory:Sound = null;
         var rec:Rectangle = null;
         var isbig:Boolean = false;
         var isCustomize:Boolean = false;
         try
         {
            request = new URLRequest(RecommendDef.SOUND_URL);
            soundFactory = new Sound();
            soundFactory.load(request);
            this._song = soundFactory.play();
         }
         catch(name_10:Error)
         {
         }
         var i:uint = 0;
         while(i < this._recommendData.length)
         {
            rec = PlayFinishRecommendUtils.getRecommendItemRectangle(i);
            isbig = i == 0?true:false;
            isCustomize = i == this._recommendData.length - 1?true:false;
            item = new PlayFinishRecommendItem(this._recommendData[i],rec.width,rec.height,isbig,isCustomize);
            item.x = rec.x;
            item.y = rec.y + BTN_HEIGHT;
            this._recommendVector.push(item);
            addChild(item);
            item.addEventListener(MouseEvent.CLICK,this.onItemMouseClick);
            i++;
         }
         if(this._isCanComment)
         {
            this.addCommonBtn();
         }
         if(this._isCanShare)
         {
            this.addShareBtn();
         }
      }
      
      private function onItemMouseClick(param1:MouseEvent) : void {
         var _loc2_:PlayFinishRecommendItem = param1.currentTarget as PlayFinishRecommendItem;
         if(_loc2_.isCustomize)
         {
            dispatchEvent(new RecommendEvent(RecommendEvent.Evt_CustomizeItemClick));
         }
         else
         {
            dispatchEvent(new RecommendEvent(RecommendEvent.Evt_OpenVideo,_loc2_.data));
         }
      }
      
      private function addShareBtn() : void {
         this._shareTF = FastCreator.createLabel("分享",16777215,14);
         this._shareTF.mouseEnabled = false;
         this._shareBtn = new ShareBtn();
         this._shareBtn.addEventListener(MouseEvent.CLICK,this.onShareBtnClick);
         addChild(this._shareBtn);
         addChild(this._shareTF);
      }
      
      private function onShareBtnClick(param1:MouseEvent) : void {
         dispatchEvent(new RecommendEvent(RecommendEvent.Evt_OpenShare));
      }
      
      private function addCommonBtn() : void {
         this._commonTF = FastCreator.createLabel("评论",16777215,14);
         this._commonTF.mouseEnabled = false;
         this._commonBtn = new ReviewBtn();
         this._commonBtn.addEventListener(MouseEvent.CLICK,this.onCommonBtnClick);
         addChild(this._commonBtn);
         addChild(this._commonTF);
      }
      
      private function onCommonBtnClick(param1:MouseEvent) : void {
         dispatchEvent(new RecommendEvent(RecommendEvent.Evt_Comment));
      }
      
      public function resize(param1:Number, param2:Number) : void {
         var _loc3_:Point = PlayFinishRecommendUtils.getShowPoint(param1,param2);
         this._showHeight = BTN_HEIGHT + _loc3_.x * (RecommendDef.PLAY_FINISH_SMALL_ITEM_HEIGHT + RecommendDef.PLAY_FINISH_ITEM_GAP);
         this._showWidth = _loc3_.y == 0?200:_loc3_.y * (RecommendDef.PLAY_FINISH_SMALL_ITEM_WIDTH + RecommendDef.PLAY_FINISH_ITEM_GAP);
         if(this._commonBtn)
         {
            this._commonTF.x = this._showWidth - this._commonTF.width - 35;
            this._commonTF.y = 5;
            this._commonBtn.x = this._commonTF.x - this._commonBtn.width + 35;
            this._commonBtn.y = 8;
         }
         if(this._shareBtn)
         {
            this._shareTF.x = this._commonBtn?this._commonBtn.x - this._shareTF.width - 10:this._showWidth - this._shareTF.width - 35;
            this._shareTF.y = 5;
            this._shareBtn.x = this._shareTF.x - this._shareBtn.width + 40;
            this._shareBtn.y = 8;
         }
         var _loc4_:* = 0;
         while(_loc4_ < this._recommendVector.length)
         {
            this._recommendVector[_loc4_].visible = true;
            if(this._recommendVector[_loc4_].row > _loc3_.x || this._recommendVector[_loc4_].col > _loc3_.y)
            {
               this._recommendVector[_loc4_].visible = false;
            }
            _loc4_++;
         }
      }
      
      public function destroy() : void {
         var _loc1_:PlayFinishRecommendItem = null;
         this._song = null;
         if((this._commonBtn) && (this._commonBtn.parent))
         {
            this._shareBtn.addEventListener(MouseEvent.CLICK,this.onShareBtnClick);
            removeChild(this._shareBtn);
            this._shareBtn = null;
         }
         if((this._shareTF) && (this._shareTF.parent))
         {
            removeChild(this._shareTF);
            this._shareTF = null;
         }
         if((this._commonBtn) && (this._commonBtn.parent))
         {
            this._commonBtn.removeEventListener(MouseEvent.CLICK,this.onCommonBtnClick);
            removeChild(this._commonBtn);
            this._commonBtn = null;
         }
         if((this._commonTF) && (this._commonTF.parent))
         {
            removeChild(this._commonTF);
            this._commonTF = null;
         }
         this._recommendData = null;
         while(this._recommendVector.length > 0)
         {
            _loc1_ = this._recommendVector.shift();
            _loc1_.removeEventListener(MouseEvent.CLICK,this.onItemMouseClick);
            removeChild(_loc1_);
            _loc1_.destroy();
            _loc1_ = null;
         }
         this._recommendVector.length = 0;
         this._recommendVector = null;
      }
   }
}
